<?php

namespace App\Models;

use App\Http\Helper\Ase;

/**
 * Class WxUser
 * @property string open_id 微信Open_ID
 * @property string union_id 微信Union_ID
 * @property string avatar 用户头像
 * @property string nickname 用户昵称
 * @property boolean subscribe 是否关注公众号
 * @property integer sex 性别,1：男,2：女
 * @property integer user_id 用户表ID
 * @property string source 来源
 * @property string channel 渠道
 */
class NewWxUser extends ApplicationModel
{
    protected $fillable = [
        'user_id',
        'open_id',
        'union_id',
        'avatar',
        'nickname',
        'subscribe',
        'sex',
        'channel',
    ];

    public $phone;

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function saveUser()
    {
        if (!$this->user_id) {
            $user = new User();
            $user->name = $this->nickname;
            $user->password = Ase::encrypt($this->open_id, '11' . time());
            $user->avatar = $this->avatar;
            $user->phone = $this->phone;
            $user->save();
            $this->user_id = $user->id;
            $this->save();
        }
    }
}
